using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using SheLife.Application.Interfaces;
using SheLife.Domain.Entities;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace SheLife.Api.Controllers
{
    [ApiController]
    [Route("api/[controller]")]
    [Authorize]
    public class MenstrualCycleController : ControllerBase
    {
        private readonly IMenstrualCycleService _menstrualCycleService;

        public MenstrualCycleController(IMenstrualCycleService menstrualCycleService)
        {
            _menstrualCycleService = menstrualCycleService;
        }

        [HttpGet("{id}")]
        public async Task<ActionResult<MenstrualCycle>> GetMenstrualCycle(int id)
        {
            var cycle = await _menstrualCycleService.GetMenstrualCycleByIdAsync(id);
            if (cycle == null)
            {
                return NotFound();
            }
            return cycle;
        }

        [HttpGet("user/{userId}")]
        public async Task<ActionResult<IEnumerable<MenstrualCycle>>> GetMenstrualCyclesByUserId(int userId)
        {
            var cycles = await _menstrualCycleService.GetMenstrualCyclesByUserIdAsync(userId);
            return Ok(cycles);
        }

        [HttpGet("user/{userId}/latest")]
        public async Task<ActionResult<MenstrualCycle>> GetLatestMenstrualCycle(int userId)
        {
            var cycle = await _menstrualCycleService.GetLatestMenstrualCycleAsync(userId);
            if (cycle == null)
            {
                return NotFound();
            }
            return Ok(cycle);
        }

        [HttpGet("user/{userId}/daterange")]
        public async Task<ActionResult<IEnumerable<MenstrualCycle>>> GetMenstrualCyclesByDateRange(
            int userId, 
            [FromQuery] DateTime startDate, 
            [FromQuery] DateTime endDate)
        {
            var cycles = await _menstrualCycleService.GetMenstrualCyclesByDateRangeAsync(userId, startDate, endDate);
            return Ok(cycles);
        }

        [HttpGet("user/{userId}/stats")]
        public async Task<ActionResult<object>> GetMenstrualCycleStats(int userId)
        {
            var avgCycleLength = await _menstrualCycleService.GetAverageCycleLengthAsync(userId);
            var avgPeriodLength = await _menstrualCycleService.GetAveragePeriodLengthAsync(userId);
            var nextPeriod = await _menstrualCycleService.PredictNextPeriodAsync(userId);

            return Ok(new
            {
                AverageCycleLength = avgCycleLength,
                AveragePeriodLength = avgPeriodLength,
                NextPredictedPeriod = nextPeriod
            });
        }

        [HttpGet("user/{userId}/ovulation/{date}")]
        public async Task<ActionResult<bool>> IsOvulationDay(int userId, DateTime date)
        {
            var isOvulationDay = await _menstrualCycleService.IsOvulationDayAsync(userId, date);
            return Ok(isOvulationDay);
        }

        [HttpPost]
        public async Task<ActionResult<MenstrualCycle>> CreateMenstrualCycle(MenstrualCycle cycle)
        {
            var createdCycle = await _menstrualCycleService.CreateMenstrualCycleAsync(cycle);
            return CreatedAtAction(nameof(GetMenstrualCycle), new { id = createdCycle.Id }, createdCycle);
        }

        [HttpPut("{id}")]
        public async Task<IActionResult> UpdateMenstrualCycle(int id, MenstrualCycle cycle)
        {
            if (id != cycle.Id)
            {
                return BadRequest();
            }

            await _menstrualCycleService.UpdateMenstrualCycleAsync(cycle);
            return NoContent();
        }

        [HttpDelete("{id}")]
        public async Task<IActionResult> DeleteMenstrualCycle(int id)
        {
            await _menstrualCycleService.DeleteMenstrualCycleAsync(id);
            return NoContent();
        }
    }
}